package com.example.demo.filter;

import com.example.demo.constent.RequestContext;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Component
public class RequestFilter extends OncePerRequestFilter  implements Ordered {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        RequestContext.setCurrentRequest(request);
        try {
            filterChain.doFilter(request, response);
        } finally {
            RequestContext.clearCurrentRequest();
        }
    }

    @Override
    public int getOrder() {
        return 1;
    }
}

